package com.loongtech.bi.action.client;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.client.EventAnalysisToolManager;
import com.loongtech.bi.manager.client.EventAnalysisToolManager.EventAnalysisData;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.NumberUtil;
import com.loongtech.core.util.Time;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * 客户端分析 —事件分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class IterativeEventClientAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private Map<String, String> enterItmes = GameParamConfigManager.getItems(CommonGameParam.EVENT_ENTER_STEPCODE); // 进入步骤
	private Map<String, String> newUserEnterItemsRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.EVENT_NEWUSER_STEPCODE); // 新用户进入步骤
	private Map<String, String> allEnterItemsRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.EVENT_ALLUSER_STEPCODE); // 全部用户进入步骤

	private int enterId = CommonGameParam.EVENT_NEWUSER_STEPCODE.COMMONID; //进入步骤类型
	private String dateRadio = "1";
	private List<EventAnalysisData> dataList1;  // 前端要显示的数据1
	private ChartUIData chartData1;  // 前端要显示的柱状图1数据
	private ChartUIData chartData2;  // 前端要显示的柱状图1数据

    private List<EventAnalysisToolManager.EventAnalysisDataChannel> dataListChannel;  // 渠道通过率数据
    private ChartUIData chartDataChannel;  // 渠道通过率图表

    private EventAnalysisToolManager.EventAnalysisDataChannel selectedData;

	@Autowired
	private EventAnalysisToolManager eventAnalysisToolManager;
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
		//删除全部
		enterItmes.remove("全部");
		search();
	}
	
	/**
	 * 切换天数
	 */
	public void radioChange() {
		search();
	}
	
	//获得商城名称
	public String getStepCodeName(Object itemType) {
		String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
		return (allEnterItemsRevise.get(key) == null) ? key : allEnterItemsRevise.get(key); 
	}

	public void search() {
        //不是自定义时间
		if(!"0".equals(dateRadio)) {
			dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - NumberUtil.parseInt(dateRadio) * Time.DAY));
			dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));
		}
		
		sysLogManager.addLog(session.getUserName(), "查询玩家分析-事件分析");
		chartData1 = null;
		chartData2 = null;
        chartDataChannel = null;
		
		dataList1 = eventAnalysisToolManager.getAllData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,newUserEnterItemsRevise,allEnterItemsRevise,enterId);
		
		if (dataList1 != null && dataList1.size() > 0) {
			List<EventAnalysisToolManager.EventAnalysisData1> list1 = new ArrayList<>();
			List<EventAnalysisToolManager.EventAnalysisData2> list2 = new ArrayList<>();

			for(EventAnalysisData data : dataList1) {
				EventAnalysisToolManager.EventAnalysisData1 data1 = eventAnalysisToolManager.new EventAnalysisData1();
				try {
					BeanUtils.copyProperties(data1, data);
				} catch (IllegalAccessException | InvocationTargetException e) {
					e.printStackTrace();
				}
				//设置名称
				data1.setStepcodeName(getStepCodeName(data1.getStepcode()));
				list1.add(data1);
				
				EventAnalysisToolManager.EventAnalysisData2 data2 = eventAnalysisToolManager.new EventAnalysisData2();
				try {
					BeanUtils.copyProperties(data2, data);
				} catch (IllegalAccessException | InvocationTargetException e) {
					e.printStackTrace();
				}
				//设置名称
				data2.setStepcodeName(getStepCodeName(data2.getStepcode()));
				list2.add(data2);
			}
			
			Collections.reverse(list1);
			Collections.reverse(list2);

            //构造柱形图
            chartData1 = new ChartUIData();
            chartData1.setJsonType(ChartType.COLUMN.type);
			chartData1.setJsonStr(new ChartTool(list1, EventAnalysisToolManager.EventAnalysisData1.class, "").loadData(chartData1.getJsonType()));
			chartData1.setAllItmes(ChartTool.getAllItems(EventAnalysisToolManager.EventAnalysisData1.class));

			//构造折线图
			chartData2 = new ChartUIData();
			chartData2.setJsonType(ChartType.SPLINE.type);
			chartData2.setJsonStr(new ChartTool(list2, EventAnalysisToolManager.EventAnalysisData2.class, "").loadData(chartData2.getJsonType()));
			chartData2.setAllItmes(ChartTool.getAllItems(EventAnalysisToolManager.EventAnalysisData2.class));
		}

        dataListChannel = eventAnalysisToolManager.getAllChannelData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, newUserEnterItemsRevise, allEnterItemsRevise, enterId);
        if (dataListChannel != null && dataListChannel.size() > 0) {
            //构造柱形图
            chartDataChannel = new ChartUIData();
            chartDataChannel.setJsonType(ChartType.COLUMN.type);
            chartDataChannel.setJsonStr(new ChartTool(dataListChannel.size() > 20 ? dataListChannel.subList(0, 20) : dataListChannel, EventAnalysisToolManager.EventAnalysisDataChannel.class, "").loadData(chartDataChannel.getJsonType(), true));
            chartDataChannel.setAllItmes(ChartTool.getAllItems(EventAnalysisToolManager.EventAnalysisDataChannel.class));
            System.out.println(chartDataChannel.getJsonStr());
        }
    }

	@Override
	protected String csvContent() {
		return "事件分析";
	}

	@Override
    protected String csvFileName() {
	    return "事件分析";
    }

	public List<EventAnalysisData> getDataList1() {
		return dataList1;
	}

	public void setDataList1(List<EventAnalysisData> dataList1) {
		this.dataList1 = dataList1;
	}

	public ChartUIData getChartData1() {
		return chartData1;
	}

	public void setChartData1(ChartUIData chartData1) {
		this.chartData1 = chartData1;
	}

	public String getDateRadio() {
		return dateRadio;
	}

	public void setDateRadio(String dateRadio) {
		this.dateRadio = dateRadio;
	}

	public int getEnterId() {
		return enterId;
	}

	public void setEnterId(int enterId) {
		this.enterId = enterId;
	}

	public Map<String, String> getEnterItmes() {
		return enterItmes;
	}

	public void setEnterItmes(Map<String, String> enterItmes) {
		this.enterItmes = enterItmes;
	}

	public ChartUIData getChartData2() {
		return chartData2;
	}

	public void setChartData2(ChartUIData chartData2) {
		this.chartData2 = chartData2;
	}

    public List<EventAnalysisToolManager.EventAnalysisDataChannel> getDataListChannel() {
        return dataListChannel;
    }

    public void setDataListChannel(List<EventAnalysisToolManager.EventAnalysisDataChannel> dataListChannel) {
        this.dataListChannel = dataListChannel;
    }

    public ChartUIData getChartDataChannel() {
        return chartDataChannel;
    }

    public void setChartDataChannel(ChartUIData chartDataChannel) {
        this.chartDataChannel = chartDataChannel;
    }

    public EventAnalysisToolManager.EventAnalysisDataChannel getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(EventAnalysisToolManager.EventAnalysisDataChannel selectedData) {
        this.selectedData = selectedData;
    }
}
